/*
 * scumix.swat.spark.TreeList
 * div 树型列表
 *
 * author: scumyang
 * create: 2008-12-01
 * update: 2009-09-13
 * notes : 需导入 scumix.swat.swat3.js
 */

/*
用法:

1.准备数据源
var	DP_TreeList =
{
	id:'mn1', hotTrack:1, rowClick:1, expand:0,
	nodes:[
	{
		c:'系统管理', url:'layout.html',
			nodes:[
			{
				c:'参数设置', url:'', expand:0,
					nodes:[
					{c:'运行参数', url:'pages/evtParam.jsp' },
					{ c:'快捷键', url:'pages/rtError.jsp' }]
			},

			{c:'帐号管理', url:'pages/dataMgr.jsp?moduleName=account'}]
	},

	{
		c:'日志管理', url:'',
			nodes:
			[
                { c:'服务器日志', url:'' },
                { c:'登录日志', url:'' }
            ]
	}]
}


2.声明变量:
var g_treeList = null;

2.初始化:
$.pageReady(function()
{
	// X1 为容器元素 id
	g_treeList = new scumix.swat.spark.TreeList( DP_TreeList, 'X1' );
});
*/

//Namespace.register( 'scumix.swat.spark.TreeList' );
scumix.swat.spark.TreeList = function( P, eContainer )
{
	this.ElName = P.id || 'SxTreeList';
	this.itemLeftGap = 0;
	this.data = P;
	this.onChange = null;
	this.onExpand = null;
    this.selectedIndex = 0;

	var o = $.createElement({type:'div', id:this.ElName, css:'', attr:{className:"SxTreeList_Root"}});
	o.absIndex = 0;
	this.rootEl = o;

	//if( d.showRoot == false ) $(o).hide();

	this.createNode( P.nodes, o, 1 );
	
	var od = $E( eContainer );
	if( od ) od.appendChild( o );
}

scumix.swat.spark.TreeList.prototype =
{
	_q: this,
	_absIndex: 0,

	createNode: function( d, p, z )
	{
		if(!d || !p) return false;
		z = z || 1;

		for( var i=0; i<d.length; i++ )
		{
			if( !d[i] ) return;

			// 条目外框
			var it = document.createElement('DIV');
			it.absIndex = ++this._absIndex;
			it.level = z;
			it.status = 1;
            it.text = d[i].c;
			it.url = d[i].url;
			it.className = 'SxTreeList_node';
			it.TLData = d[i];
			it.jsObject = this;

			// 条目标题
			var it_caption = document.createElement('DIV');
            it_caption.id = "TL_NC_" + it.absIndex;
			it_caption.className = 'SxTreeList_node_caption';
			it_caption.exItemEl = it;
			it.appendChild( it_caption );

			// 跟踪热点
			if( this.data.hotTrack )
			{
				$(it_caption).addEvent( 'mouseover', this.doNLOver );
				$(it_caption).addEvent( 'mouseout', this.doNLOut );
			}

			// 整行点击
			if( this.data.rowClick )
			{
				$(it_caption).addEvent( 'click', this.doNLClick );
			}

			// node icon
			var ni = 0;
			if( d[i].icon )
			{
				ni = document.createElement('div');
				ni.className = 'SxTreeList_ni';
				ni.style.background = 'url(' + d[i].icon + ') no-repeat center';
				$(ni).addEvent( 'click', this.doNTClick );
			}

			// child-item
			if( d[i].nodes )
			{
				// node switch icon
				var nsi = document.createElement('div');

				if( !this.data.expand || d[i].expand == 0 )
				{
					nsi.className = 'SxTreeList_nsi SxTreeList_nsi_c';
					it.status = 0;
				}
				else nsi.className = 'SxTreeList_nsi SxTreeList_nsi_e';

				nsi.exItemEl = it;
				$(nsi).addEvent( 'mouseover', this.doNIOver );
				$(nsi).addEvent( 'mouseout', this.doNIOut );
				$(nsi).addEvent( 'click', this.doNIClick );
				it_caption.appendChild( nsi );
				it_caption.style.paddingLeft = (z-1) * 18 + this.itemLeftGap;

				// node content
				var it_content = document.createElement('DIV');
				it_content.id = this.elName + '_ITC' + it.absIndex;
				it_content.className = 'SxTreeList_node_content';
				if( d[i].css ) it_content.style.cssText = d[i].css;
				if( !this.data.expand || d[i].expand == 0 ) it_content.style.height = 1;
				$(it_content).css('opacity',100);
				it.appendChild( it_content );
				it.exContentEl = it_content;
			}
			else
			{
				if( ni ) it_caption.style.paddingLeft = (it.level-1) * 18 + this.itemLeftGap + 16;
				else it_caption.style.paddingLeft = (it.level-1) * 18 + this.itemLeftGap + 18;
			}

			if( ni ) it_caption.appendChild( ni );

			// node text
			var nt = document.createElement('div');
			nt.className = 'SxTreeList_nt';
			if( d[i].css )
			{
				nt.style.cssText = d[i].css;
			}

			$(nt).setText( d[i].c );
			$(nt).addEvent( 'mouseover', this.doNTOver );
			$(nt).addEvent( 'mouseout', this.doNTOut );
			$(nt).addEvent( 'click', this.doNTClick );
			it_caption.appendChild( nt );

			p.appendChild( it );

			if( d[i].nodes ) this.createNode( d[i].nodes, it_content, z + 1 );
		}

		return true;
	},

	// NodeLine
	doNLOver: function()
	{
		var jso = this.parentNode;
        if( jso.jsObject.selectedIndex != jso.absIndex ) this.className = 'SxTreeList_node_captionH';
	},

	doNLOut: function()
	{
		var jso = this.parentNode;
        if( jso.jsObject.selectedIndex != jso.absIndex ) this.className = 'SxTreeList_node_caption';
		else this.className = 'SxTreeList_node_captionS';
	},

	doNLClick: function()
	{
		var o = this, p = o.exItemEl;
		if( !p ) return false;
		//if( p.TLData.onClick ) p.TLData.onClick( p );
		return true;
	},

	// NodeText
	doNTOver: function()
	{
		var o = this, p = o.parentNode;
		if( !p ) return false;
		o.className = 'SxTreeList_ntH';
		return true;
	},

	doNTOut: function()
	{
		var o = this, p = o.parentNode;
		if( !p ) return false;
		o.className = 'SxTreeList_nt';
		return true;
	},

	doNTClick: function( s )
	{
		var o = this, p = o.parentNode.parentNode;
		if( !p ) return false;
		
		if( p.jsObject.onChange ) p.jsObject.onChange( p );
		if( p.TLData.onClick ) p.TLData.onClick( p );
        var el = $E( "TL_NC_" + p.jsObject.selectedIndex );
        if( el ) el.className = "SxTreeList_node_caption";
        p.jsObject.selectedIndex = p.absIndex;
		return true;
	},

	// NodeIcon
	doNIOver: function()
	{
		var o = this, p = o.exItemEl;
		if( !p ) return false;
		switch( p.status )
		{
			case 0: o.className = 'SxTreeList_nsi SxTreeList_nsi_cH'; break;
			case 1: o.className = 'SxTreeList_nsi SxTreeList_nsi_eH'; break;
		}
		return true;
	},

	doNIOut: function()
	{
		var o = this, p = o.exItemEl;
		if( !p ) return false;
		switch( p.status )
		{
			case 0: o.className = 'SxTreeList_nsi SxTreeList_nsi_c'; break;
			case 1: o.className = 'SxTreeList_nsi SxTreeList_nsi_e'; break;
		}
		return true;
	},

	doNIClick: function()
	{
		var o = this, p = o.exItemEl, pc = p.exContentEl;
		if(!p) return false;
		switch( p.status )
		{
			case 1:
				if( pc )
				{
					p.status = 0;
					o.className = 'SxTreeList_nsi SxTreeList_nsi_cH';
					pc.style.height = pc.offsetHeight;
					$(pc).anim({height:1,opacity:0},5,function(){pc.style.height='1'});
				}
				break;

			case 0:
				if( pc )
				{
					p.status = 1;
					pc.style.height = 'auto';
					var n = pc.offsetHeight;
					pc.style.height = 20;
					pc.parentNode.style.height='auto';
					o.className = 'SxTreeList_nsi SxTreeList_nsi_eH';
					$(pc).anim({height:n,opacity:99},5,function(){pc.style.height = 'auto'});
				}
				break;
		}

		var jso = o.parentNode.parentNode.jsObject;
		if( jso && jso.onExpand ) jso.onExpand( o, jso );

		return true;
	}
}